package com.hitqz.robot.biz.flow.component.agv;

import cn.hutool.json.JSONObject;
import com.hitqz.robot.api.common.entity.RobotMapPos;
import com.hitqz.robot.biz.business.dispatch.DispatchService;
import com.hitqz.robot.biz.flow.domain.constant.FlowConstant;
import com.hitqz.robot.biz.flow.util.LiteFlowUtil;
import com.hitqz.robot.biz.service.BusinessService;
import com.hitqz.robot.biz.service.RobotMapPosService;
import com.hitqz.robot.biz.service.RobotService;
import com.hitqz.robot.driver.parent.RobotDriverService;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeComponent;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @Author: Wang Weiwei
 * @Description: TODO2
 * @DateTime: 2024/5/6 10:44
 **/
@Slf4j
@LiteflowComponent(value = "GoWaitForStandBy", name = "[轮对/轮轴]前往站点-等待区域")
@AllArgsConstructor
public class GoWaitForStandBy extends NodeComponent {
    private final BusinessService businessService;

    private final RobotDriverService robotDriverService;

    private final RobotService robotService;

    private final RobotMapPosService robotMapPosService;

    private final DispatchService dispatchService;



    @Override
    public void process() throws Exception {
        LiteFlowUtil.checkInterruptFlag();
        String key = FlowConstant.NODE_CUSTOM_INFO_MAP.get(this.getNodeId()).getParamNames().get(0);
        Integer cmd = this.getCmpData(JSONObject.class).get(key, Integer.class);
        cmd = Math.max(0,cmd);
        log.info("[liteFlow]等待位置:{}",cmd);
        RobotMapPos robotMapPos = robotMapPosService.getById(cmd);
        // 为了防止调度出现问题，返回等待点才回将状态清空, 由于位置很小
        dispatchService.go2TargetPosition(robotMapPos, false);
        //TimeUnit.SECONDS.sleep(3);
        //IRobotPosDto iRobotPosDto = robotDriverService.getWebPos();
        //while (true) {
        //    try {
        //        iRobotPosDto = robotDriverService.getWebPos();
        //        if (iRobotPosDto != null && iRobotPosDto.getCurrentStation().equals(robotMapPos.getCode()) && iRobotPosDto.isReached()) {
        //            log.info("机器人到达位置");
        //            break;
        //        }
        //        LiteFlowUtil.checkInterruptFlag();
        //        TimeUnit.SECONDS.sleep(1);
        //    }catch (FlowInterruptException e) {
        //        break;
        //    }
        //}
        //log.info("起始点机器人到达位置了");
        //iRobotPosDto = robotDriverService.getWebPos();

    }
}